function SimpleReplyComment(param) {

	var that = this;

	var RETRIEVE_COMMENTS_URL = "/zzzzzz/user/retrieve_comments";
	var DELETE_COMMENT_URL = "/zzzzzz/user/delete_comment";
	
	this.j_comment_entry_container = null;
	this.j_comment_post_form = null;
	this.j_comment_post_textarea = null;
	this.j_comment_reply_textarea = null;
	this.j_comment_entry_content = null;
	this.j_comment_showing_status = null;
	this.j_comment_unshown_status = null;
	this.j_comment_loading_status = null;
	this.j_comment_entry_options = null;
	this.j_view_more_container = null;
	this.j_view_more_button = null;
	this.j_no_more_result = null;
	this.j_reply_link = null;

	var topic_id = 0;
	var offset = 0;
	var cur_page_no = 0;
	
	var cur_showing_no = 0;
	var total = 0;
	
	
	var jqxhr_retrieve_comments = null;
	
	var comments_retrieving = false;
	
	var has_more_result = true;
	
	function __construct(p) {
	try{
		for(pkey in p) {
			if(pkey[0] != "_") {
				that[pkey] = p[pkey];
			}
		}
		
		if(p._topic_id) {
			topic_id = p._topic_id;
		}
		if(p._offset) {
			offset = p._offset;
		}
		
		that.j_comment_post_textarea.live('focus', postTextareaFocus).live('focusout', postTextareaFocusout).live('keydown', postTextareaKeydown);
		that.j_comment_reply_textarea.live('focus', replyTextareaFocus).live('focusout', replyTextareaFocusout).live('keydown', replyTextareaKeydown);
		that.j_view_more_button.click(viewMoreClick);
		that.j_comment_entry_content.live('mouseenter', entryContentMouseenter).live('mouseleave', entryContentMouseleave);
		that.j_post_link.live('click', postLinkClick);
		that.j_reply_link.live('click', replyLinkClick);
		that.j_comment_entry_options.live('click', commentEntryCloseClick);
		
	}catch(err){
		alert(err);
	}
	}
	
	function entryContentMouseenter(e) {
	try{
		jq(this).find("[data-comment-dom-id=entry-options]").show();
	}catch(err){
		alert(err);
	}
	}
	
	function entryContentMouseleave(e) {
	try{
		jq(this).find("[data-comment-dom-id=entry-options]").hide();
	}catch(err){
		alert(err);
	}
	}

	function postTextareaFocus(e) {
	try{
		var post_textarea = jq(this);
		post_textarea.css({ "height" : "28px" });
		var form_container = post_textarea.closest("[data-comment-dom-id=post-form-container]");
		form_container.find("[data-comment-dom-id=avatar]").show();
		form_container.find("[data-comment-dom-id=post-tip]").show();
		form_container.find("[data-comment-dom-id=post-textarea-container]").css({ "margin" : "0 0 0 40px" });
	}catch(err){
		alert(err);
	}
	}
	
	function postTextareaFocusout(e) {
	try{
		var post_textarea = jq(this);
		var val = jq.trim(post_textarea.val());
		if(val.length == 0) {
			post_textarea.val("").css({ "height" : "14px" });
			var form_container = post_textarea.closest("[data-comment-dom-id=post-form-container]");
			form_container.find("[data-comment-dom-id=avatar]").hide();
			form_container.find("[data-comment-dom-id=post-tip]").hide();
			form_container.find("[data-comment-dom-id=post-textarea-container]").css({ "margin" : "0" });
		}
	}catch(err){
		alert(err);
	}
	}
	
	function postTextareaKeydown(e) {
	try{
		if(e.which == 13) {
			e.preventDefault();
			var val = jq.trim(jq(this).val());
			if(val.length > 0) {
				addComment();
			}
		}
	}catch(err){
		alert(err);
	}
	}
	
	function replyTextareaFocus(e) {
	try{
		jq(this).closest("[data-comment-dom-id=reply-form-container]").show();
	}catch(err){
		alert(err);
	}
	}
	
	function replyTextareaFocusout(e) {
	try{
		var reply_textarea = jq(this);
		var val = jq.trim(reply_textarea.val());
		if(val.length == 0) {
			reply_textarea.val("");
			reply_textarea.closest("[data-comment-dom-id=reply-form-container]").hide();
		}
	}catch(err){
		alert(err);
	}
	}
	
	function replyTextareaKeydown(e) {
	try{
		if(e.which == 13) {
			e.preventDefault();
			var reply_textarea = jq(this);
			var val = jq.trim(reply_textarea.val());
			if(val.length > 0) {
				addReply(reply_textarea);
			}
		}
	}catch(err){
		alert(err);
	}
	}
	
	function replyLinkClick(e) {
	try{
		e.preventDefault();
		var entry = jq(this).closest("[data-comment-is-parent=true]");
		entry.find("[data-comment-dom-id=reply-textarea]").focus();
	}catch(err){
		alert(err);
	}	
	}
	
	function postLinkClick(e) {
	try{
		e.preventDefault();
		that.j_comment_post_textarea.focus();
		
	}catch(err){
		alert(err);
	}	
	}
	
	function retrieve() {
	try{
		if(comments_retrieving) {
			jqxhr_retrieve_comments.abort();
		}
		comments_retrieving = true;
		that.j_comment_loading_status.show();
		jqxhr_retrieve_comments = jq.post(RETRIEVE_COMMENTS_URL, { "topic_id": topic_id, "cur_showing_no": cur_showing_no, "offset": offset }, retrieveSuccess)
		.error(function() {
	    	alert("Oops! An error occurred while processing your request.");
	    })
	    .complete(function() {
	   		// complete
			that.j_comment_loading_status.hide();
			comments_retrieving = false;
	    });
	}catch(err){
		alert(err);
	}
	}
	
	function retrieveSuccess(data) {
	try{
		total = data.total;
		cur_showing_no += data.items.length;
		updateShowingStatus();
		
		jq.each(data.items, function(index, value) {
			jq(value.html).insertBefore(that.j_view_more_container);
		});
		
		if(cur_showing_no < total) {
			that.j_comment_unshown_status.text(getUnshowingStatus());
			that.j_view_more_container.show();
		}
		else if(cur_page_no > 0){			
			that.j_no_more_result.show();
		}
		cur_page_no += 1;
	}catch(err){
		alert(err);
	}
	}
	
	function addComment() {
	try{
		var url = that.j_comment_post_form.attr("action");
		var content = that.j_comment_post_textarea.val();
		jq.get(url, { "topic_id": topic_id, "content": content }, addCommentSuccess)
		.error(function() {
	    	alert("Oops! An error occurred while processing your request.");
	    })
	    .complete(function() {
	   		// complete
	    });

	}catch(err){
		alert(err);
	}
	}
	
	function addCommentSuccess(data) {
	try{
		that.j_comment_post_textarea.val("").focusout();
		cur_showing_no += 1;
		total += 1;
		updateShowingStatus();
		jq(data).insertAfter(that.j_comment_showing_status);
	}catch(err){
		alert(err);
	}
	}
	
	function addReply(reply_textarea) {
	try{
		var content = reply_textarea.val();
		var parent_entry = reply_textarea.closest("[data-comment-is-parent=true]");
		var reply_form = parent_entry.find("[data-comment-dom-id=reply-form]");
		var url = reply_form.attr("action");

		var parent_id = parent_entry.data("comment-id");
		
		jq.get(url, { "topic_id": topic_id, "parent_id": parent_id, "content": content }, addReplySuccess)
		.error(function() {
	    	alert("Oops! An error occurred while processing your request.");
	    })
	    .complete(function() {
	   		// complete
	    });

	}catch(err){
		alert(err);
	}
	}
	
	function addReplySuccess(data) {
	try{
		var parent_id = data.parentId;
		var parent_entry = that.j_comment_entry_container.find("[data-comment-id=" + parent_id + "]");
		parent_entry.find("[data-comment-dom-id=reply-textarea]").val("").focusout();
		jq(data.html).insertBefore(parent_entry.find("[data-comment-dom-id=reply-form-container]"));
	}catch(err){
		alert(err);
	}
	}

	function updateShowingStatus() {
	try{
		that.j_comment_showing_status.text(getShowingStatus());
		if(!that.j_comment_showing_status.is(":visible")) {
			that.j_comment_showing_status.show();
		}
	}catch(err){
		alert(err);
	}
	}
	
	function commentEntryCloseClick(e) {
	try{
		e.preventDefault();
		var comment_entry = jq(this).closest("[data-comment-id]");
		var comment_id = comment_entry.data("comment-id");
		var is_parent = comment_entry.data("comment-is-parent");
		deleteComment(comment_id, is_parent);
	}catch(err){
		alert(err);
	}
	}
	
	function deleteComment(comment_id, is_parent) {
	try{
		if(confirm("Are you sure to delete this comment.")) {	
			comment_deleting = true;
			jq.get(DELETE_COMMENT_URL, { "comment_id": comment_id, "is_parent" : is_parent }, deleteCommentSuccess)
			.error(function() {
	    		alert("Oops! An error occurred while processing your request.");
	    	})
	    	.complete(function() {
	   			// complete
	    		comment_deleting = false;
	    	});
		}
	}catch(err){
		alert(err);
	}
	}
	
	function deleteCommentSuccess(data) {
	try{
		var comment_id = data.commentId;
		that.j_comment_entry_container.find("[data-comment-id=" + comment_id + "]").remove();
		if(data.isParent) {
			cur_showing_no -= 1;
			total -= 1;
			updateShowingStatus();
		}
	}catch(err){
		alert(err);
	}
	}
	
	function getShowingStatus() {
	try{
		if(total === 0) {
			return "No comment yet";
		}else {
			if(cur_showing_no === total) {
				if(total === 1) {
					return "Showing 1 comment";
				}else {
					return "Showing " + cur_showing_no + " comments";
				}
			}else {
				return "Showing " + cur_showing_no + " of " + total + " comments";
			}
		}
	}catch(err){
		alert(err);
	}
	}
	
	function getUnshowingStatus() {
	try{
		return total-cur_showing_no;
	}catch(err){
		alert(err);
	}
	}
	
	function viewMoreClick(e) {
	try{	
		e.preventDefault();
		that.j_view_more_container.hide();
		retrieve();
	}catch(err){
		alert(err);
	}
	}
	
	
	__construct(param);

	retrieve();
	
}

